<?php
namespace addon\friend_link;

use app\common\controller\AddonBase;

use app\common\dao\AddonInterface;

use tpfcore\Core;

use think\Db;

class FriendLink extends AddonBase implements AddonInterface
{
    private $addonName="友情链接模块";
    /**
     * 实现钩子
     */
    public function RobotApplication($param = [])
    {
        $this->addonTemplate(':run',[
            "name"=>$param['name'],
        ]);
    }
    
    /**
     * 插件安装
     */
    public function addonInstall()
    {
        try{
            $this->addonInstallBefore();

            return [RESULT_SUCCESS, $this->addonName.'安装成功'];

        }catch(\Exception $e){

            throw new \Exception($e->getMessage());

        }
    }
    
    /**
     * 插件卸载
     */
    public function addonUninstall()
    {
        
       try{
            $this->addonUninstallBefore();
            return [RESULT_SUCCESS, $this->addonName.'卸载成功'];
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
    
    /**
     * 插件基本信息
     */
    public function addonInfo()
    {
        return [
            'name' => 'friend_link',
            'title' => '友情链接', 
            'describe' => '友情链接管理模块', 
            'author' => 'yaosean', 
            'version' => '2.0' ,
            'require'=>'>=3.0', 
            "config" => false,
            'type'=>'module'
        ];
    }
    
    /**
     * 插件配置信息
     */
    public function addonConfig()
    {
        
       
    }
    /**
     * 插件安装前的一些操作
     */
    public function addonInstallBefore(){
        try{
            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='插件模块'");
            $parentid=$result[0]['id'];
            
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('友情链接', 'friend_link', '', '', '1', '', '1', '', 'link', '0', '$parentid');");

            $parentid=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('友情链接管理', 'friend_link', 'FriendLink', 'index', '1', '', '1', '', '', '0', '$parentid');");

        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }
        
    }
    /**
     * 卸载前的一些操作
     */
    public function addonUninstallBefore(){
        try{
            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='友情链接'");
            if(!empty($result)){
                $parentid=$result[0]['id'];
                Db::execute("DELETE FROM `".DB_PREFIX."menu` WHERE id=$parentid OR parentid=$parentid");
            }
        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }
    }
}
